package com.changan.jwtdemo.response;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 公共返回结果类
 */
@Data
public class Result {
    private Boolean success;

    private Integer code;

    private String message;

    /**
     * total : 50
     * records :{
     *     users:[
     *       {},{}
     *     ]
     * }
     */
    private Map<String, Object> data = new HashMap<String, Object>();

    /**
     * 私有化的目的是为了保护这个类中的属性
     */
    private Result(){

    }

    /**
     * this是表示本类的对象
     * 每个方法返回this的目的是为了能够进行链式操作
     */

    /**
     * 既然私有化了类,那就一定要提供公共静态的方法
     */
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("成功");
        return result;
    }

    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage("失败");
        return result;
    }

    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result message(String message){
        this.setMessage(message);
        return this;
    }

    public Result code(Integer code){
        this.setCode(code);
        return this;
    }

    public Result data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map){
        this.setData(map);
        return this;
    }
}
